package cn.woo5.test.detector.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;

@Slf4j
@RequestMapping("/test/upload")
@RestController
public class SwaggerFileUploadTestController {

    @RequestMapping(value = "multi", method = RequestMethod.POST)
    @SneakyThrows
    public ResponseEntity<Resource> multiFileUploadTest(@RequestParam("file1") MultipartFile file1, @RequestParam("file2") MultipartFile file2) {
        byte[] file1Content = file1.getBytes();
        byte[] file2Content = file2.getBytes();
        log.info("file1 length:{}", file1Content.length);
        log.info("file2 length:{}", file2Content.length);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(file1Content);
        outputStream.write(file2Content);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
        return ResponseEntity.ok().headers(headers).contentLength(resource.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream")).body(resource);
    }

}
